home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15356 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.nstn.ca!news
  2. From: mgemmell@transres.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: casting a void pointer back to a function pointer
  5. Date: Thu, 04 Apr 1996 20:48:18 GMT
  6. Organization: Nova Scotia Technology Network
  7. Message-ID: <4k1cut$75u@news.nstn.ca>
  8. References: <DLABELL.96Apr4021045@columbia.pcs.cnu.edu>
  9. NNTP-Posting-Host: pc-007.transres.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. dlabell@pcs.cnu.edu (Daniel LaBell) wrote:
  13.  
  14. >I'm going to skip explaining why I want to do this, and get right to the
  15. >problem.  How do I cast a pointer to a function pointer?
  16.  
  17. >Here is trivial example that shows the problem.
  18.  
  19. >void foo1(){  cout << " foo1 " << endl; }
  20. >void foo2(){  cout << " foo2 " << endl; }
  21. >void foo3( int x )
  22. >{ cout << " foo3, argument = " << x << endl; }
  23.  
  24. >void do_a_foo( void (*fun)() )
  25. >{  (*fun)(); }
  26. >void do_a_foo2( int x,  void (*fun) (int ) )
  27. >{  (*fun) ( x ); } 
  28. >int main()
  29. >{
  30. >  do_a_foo  ( foo1 );
  31. >  do_a_foo  ( foo2 );
  32. >  do_a_foo2 ( 2, foo3 );
  33. >  void * x=foo2;
  34. >  do_a_foo  (x); // I don't know the syntax to do this cast. :(
  35.   do_a_foo((void (*)())x);
  36. >  return 0;
  37. >}
  38. >I get this warning message from g++:
  39. >:/home/student/dlabell/C/test.cc: In function `int main()':
  40. >:/home/student/dlabell/C/test.cc:21: warning: ANSI C++ forbids implicit conversion from `void *' in argument passing
  41. >:
  42. >:Compilation finished at Thu Apr  4 01:18:20
  43.  
  44. <snip>
  45. >Daniel LaBell
  46.  
  47. Or if you want to do a typedef try the following.
  48. typedef void (*vFnNoArgs)();
  49.  
  50. then in your code you can have
  51.   do_a_foo((vFnNoArgs)x);
  52.  
  53. As far as I know this is all ANSI stuff.
  54.  
  55. Hope this helps
  56.    Mike Gemmell
  57.    mgemmell@transres.com
  58.  
  59.